﻿using System;
using System.Collections.Generic;
using System.Linq;

public class OrderService
{
    private List<Order> orders = new List<Order>();
    private int nextOrderId = 1;
    private int nextDetailId = 1;

    public void AddOrder(Order order)
    {
        if (order == null)
            throw new ArgumentNullException(nameof(order));

        order.OrderId = nextOrderId++;
        foreach (var detail in order.Details)
        {
            detail.DetailId = nextDetailId++;
        }
        orders.Add(order);
    }

    public void DeleteOrder(int orderId)
    {
        var order = orders.FirstOrDefault(o => o.OrderId == orderId);
        if (order != null)
        {
            orders.Remove(order);
        }
    }

    public void UpdateOrder(Order order)
    {
        if (order == null)
            throw new ArgumentNullException(nameof(order));

        var existing = orders.FirstOrDefault(o => o.OrderId == order.OrderId);
        if (existing != null)
        {
            orders.Remove(existing);
            orders.Add(order);
        }
    }

    public List<Order> QueryAllOrders()
    {
        return orders.OrderBy(o => o.OrderId).ToList();
    }

    public List<Order> QueryOrders(Func<Order, bool> predicate)
    {
        return orders.Where(predicate).OrderBy(o => o.OrderId).ToList();
    }

    public Order GetOrderById(int orderId)
    {
        return orders.FirstOrDefault(o => o.OrderId == orderId);
    }
}